package cn.bdqn.exception;

import cn.bdqn.model.R;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author gurilla  可以把这个东西做成一个工具类
 * @create 2019-08-11 8:45
 */
@RestControllerAdvice
public class RRExceptionHandler {
    @ExceptionHandler(DuplicateKeyException.class)
    public R handlerDuplicateKeyException(DuplicateKeyException e){
        e.printStackTrace();
        return R.error("主键重复!");
        //体验度不好，例如电话不能重复，用户名不能重复
    }

    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e){
        e.printStackTrace();
        return R.error("操作失败!");
    }

    @ExceptionHandler(NullPointerException.class)
    public R handlerNullPointerException(NullPointerException e){
        e.printStackTrace();
        return R.error("空指针异常!");
    }

    @ExceptionHandler(UnauthorizedException.class)
    public R handlerUnauthorizedException(UnauthorizedException e){
        e.printStackTrace();
        return R.error("没有权限操作!");
    }

    @ExceptionHandler(RRException.class)
    public R handlerRRException(RRException e){
        e.printStackTrace();
        return R.error(e.getMessage());
    }

    @ExceptionHandler(AuthorizationException.class)
    public R handlerAuthorizationException(AuthorizationException e){
        e.printStackTrace();
        return R.error(e.getMessage());
    }
}
